import { createRouter, createWebHashHistory } from "vue-router";
import { LOGIN_TOKEN } from '@/global/constants'
import { firstMenu } from "@/untils/map-menus";
const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/main'//重定向
    },
    {
      path: '/login',
      component: () => import('../views/login/Login.vue')
    },
    {
      path: '/main',
      name: 'main',
      component: () => import('../views/main/Main.vue')
    },
    {
      path: '/:pathMatch(.*)*',
      component: () => import('../views/not-found/NotFound.vue')
    }
  ]
})


// 导航守卫
// 参数：to(跳转到哪里) / from(从哪里跳转过来)
// 返回值：决定导航的路径
router.beforeEach((to, from) => {
  // console.log(to,from,'---dd')
  const token = localStorage.getItem(LOGIN_TOKEN)
  if (to.path === '/main' && !token) {
    // if (!localStorage.getItem('token')) return to.path = '/login'
    return '/login'
  }

  // 如果要进入main
  if(to.path === '/main'){
    return firstMenu?.url
  }
})
export default router
